package com.ruoyi.common.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// Spring Boot 3.x使用jakarta.servlet，2.x使用javax.servlet
@Component
@WebFilter(urlPatterns = "/*")
public class MapFileFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        String requestUri = httpRequest.getRequestURI();

        // 检查请求是否为.map文件
        if (requestUri != null && requestUri.endsWith(".map")) {
            httpResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
            httpResponse.getWriter().write("Access to .map files is prohibited");
            return;
        }

        // 非.map文件请求继续处理
        chain.doFilter(request, response);
    }
}
